#!/usr/bin/env bash

# do help
command_do_help()
{
    path=$1
    params=${@:2:$#-1}
    [[ -f $path ]] || return 1
    unset command_help
    source $path
    command_help $params
    unset command_help
    return 0
}

# 命令的名字。
COMMAND_NAME="help"

# 主函数，当命令被调用时执行。
command_main()
{
    help_cmd_str=$1
    help_params=${@:2:$#-1}
    if [[ -z $help_cmd_str ]]; then
        cmd_list="$(command_travel)"
        while IFS=$'\n' read -r line; do
            echo "$(command_do_help $line)"
        done <<< "$cmd_list"
    else
        if [[ -f $help_cmd_str ]]; then
            if [[ $help_cmd_str == *".sh" ]]; then
                command_do_help $help_cmd_str || return $CMD_STATUS_FAILD
            else
                debug_echo "The file suffix needs to use \".sh\""
                return $CMD_STATUS_FAILD
            fi
        else
            if r_cmd_string="$(command_search $help_cmd_str)"; then
                command_do_help $r_cmd_string $help_params || return $CMD_STATUS_FAILD
            else
                debug_echo "The \"$help_cmd_str\" command was not found!"
                return $CMD_STATUS_NOFOUND
            fi
        fi
    fi
    return 0
}

# 退出，在command_main结束后被调用。
# 一般不要在这个函数里进行耗时的操作，即使command_main被打断，系统退出前依然会执行这个函数。
command_exit()
{
    return 0
}

# 帮助信息，在help或者cmd --help时被调用。
command_help()
{
    help_print "help" "show help information"
    return 0
}
